package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.PersonalAuditBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/1/3 0003.
 */

public class PersonalAuditModel implements PersonalAuditModelImpl {

    private OnPersonalAuditResponseListener mListener;

    public PersonalAuditModel(OnPersonalAuditResponseListener listener) {
        mListener = listener;
    }

    @Override
    public void requestData(String uid, String typeName, String search, int pageSize, int page) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("type_name", typeName)
                .addFormDataPart("search", search)
                .addFormDataPart("pageSize", String.valueOf(pageSize))
                .addFormDataPart("page", String.valueOf(page))
                .build();
        Observable<PersonalAuditBean> observable = DataManager.getPersonalAuditData(body);
        Observable<PersonalAuditBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<PersonalAuditBean>() {
            @Override
            public void onNext(PersonalAuditBean bean) {
                if (mListener != null) {
                    mListener.onRequestDataSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    mListener.onRequestDataFailed(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public interface OnPersonalAuditResponseListener {
        void onRequestDataSuccess(PersonalAuditBean bean);

        void onRequestDataFailed(Throwable throwable);
    }
}
